package com.stylefeng.guns.modular.portal;
//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.modular.item.model.Item;
import com.stylefeng.guns.modular.item.pojo.ItemPojo;
import com.stylefeng.guns.modular.item.service.IItemService;
import com.stylefeng.guns.modular.item.util.Const;
import com.stylefeng.guns.modular.item.util.PageHelper;
import com.stylefeng.guns.modular.pic.model.Pic;
import com.stylefeng.guns.modular.pic.service.IPicService;
import com.stylefeng.guns.modular.quickImg.service.IQuickimgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author: yyi
 * @date: 2018/9/28 14:56
 * @parameters:
 * @desc:
 **/
@Controller
@RequestMapping(value = "/portal")
public class IndexController {
    @Autowired
    private IItemService itemService;
    @Autowired
    private IPicService picService;
    @Autowired
    private IQuickimgService quickimgService;
    private static String PREFIX ="/portal/";
    @RequestMapping(value = "/index")
    public String index(Model model){
        List list1 = itemService.getItemByCategoryOrderByDate(1);
        model.addAttribute("list1",list1);
        List list2 = itemService.getItemByCategoryOrderByDate(2);
        model.addAttribute("list2",list2);
        List list3 = itemService.getItemByCategoryOrderByDate(3);
        model.addAttribute("list3",list3);
        List list4 = itemService.getItemByCategoryOrderByDate(4);
        model.addAttribute("list4",list4);
        List list5 = itemService.getItemByCategoryOrderByDate(5);
        model.addAttribute("list5",list5);
        List list6 = itemService.getItemByCategoryOrderByDate(6);
        model.addAttribute("list6",list6);
        List list7 = itemService.getItemByCategoryOrderByDate(7);
        model.addAttribute("list7",list7);
        List list8 = itemService.getItemByCategoryOrderByDate(8);
        model.addAttribute("list8",list8);
        /*轮播图片*/
        List pic1 = picService.getPicByTypeNum(1,4);
        /*蔚蓝海洋*/
        List pic2 = picService.getPicByTypeNum(2,6);
        model.addAttribute("pic1",pic1);
        model.addAttribute("pic2",pic2);
        /*快速通道*/
        List list_quick = quickimgService.getSixQiuckImg();
        model.addAttribute("list9",list_quick);

        return PREFIX +"index.html";
    }
    /**
     * 查看详细文章
     * **/
    @RequestMapping("/detail/{id}")
    public String getDetailById(@PathVariable int id,Model model){
        ItemPojo itemPojo = new ItemPojo();
        Item item = itemService.selectById(id);
        Date date = item.getDate();
        int category = item.getCategory();
        Item next = itemService.getNextItem(category,date);
        Item previous = itemService.getPreviousItem(category,date);
        if (next!=null){
            itemPojo.setNextTitle(next.getTitle());
            itemPojo.setNextId(next.getId());
        }else{
            itemPojo.setNextTitle("无");
            itemPojo.setNextId(item.getId());
        }
        if (previous!=null){
            itemPojo.setPreviousTitle(previous.getTitle());
            itemPojo.setPreviousId(previous.getId());
        }else{
            itemPojo.setPreviousTitle("无");
            itemPojo.setPreviousId(item.getId());
        }
        itemPojo.setItem(item);
        if (item.getCategory()==1){
            itemPojo.setType("海权历史");
        }else if (item.getCategory()==2){
            itemPojo.setType("海权理论");
        }else if (item.getCategory()==3){
            itemPojo.setType("海洋制度");
        }else if (item.getCategory()==4){
            itemPojo.setType("海洋热点");
        }else if (item.getCategory()==5){
            itemPojo.setType("海洋强国战略");
        }else if (item.getCategory()==6){
            itemPojo.setType("中国海洋战略");
        }else if (item.getCategory()==7){
            itemPojo.setType("海上丝绸之路");
        }else if (item.getCategory()==8){
            itemPojo.setType("全球海洋治理");
        }
        model.addAttribute("item",itemPojo);
        return  PREFIX + "detail.html";
    }
    /**设置分页,**/
    @RequestMapping(value = "/getpageIndex/{type}/{currPage}")
    public String getPageNext(@PathVariable("type") int type, @PathVariable("currPage") int currPage, Model model, HttpServletRequest request){
        if(currPage==0){
            currPage = Integer.parseInt(request.getParameter("pageInfo"));
        }
        Page<Item> page = new Page();
        page.setCurrent(currPage);
        page.setSize(10);
        Wrapper<Item> wrapper = new EntityWrapper<>();
        wrapper.eq("category",type);
        wrapper.orderBy("date",false);
        Page<Item> pages = itemService.selectPage(page,wrapper);
        //自己分装的翻页对象
        PageHelper pageHelper = new PageHelper();
        /*当前页*/
        pageHelper.setCurrent(pages.getCurrent());
        /*总记录*/
        pageHelper.setPages(pages.getPages());
        /*总页数*/
        pageHelper.setRecords(pages.getRecords());
        List<com.stylefeng.guns.modular.item.util.Model> ls = Const.getModelList();
        for(int i=0;i<ls.size();i++){
            if(ls.get(i).getId()==type){
                pageHelper.setCategory(ls.get(i).getCategory());
            }
        }
        /*上一页页码*/
        int previousPage = pages.getCurrent()-1;
        if (previousPage==0){
            pageHelper.setPreviousPage(1);
        }else{
            pageHelper.setPreviousPage(previousPage);
        }
        /*下一页页码*/
        int nextPage = pages.getCurrent()+1;
        if(nextPage<=pages.getPages()){
            pageHelper.setNextPage(nextPage);
        }else{
            pageHelper.setNextPage((int)pages.getPages());
        }
        model.addAttribute("page",pageHelper);
        return PREFIX + "dynamic.html";

    }
    /**
    * 点击蔚蓝海洋进行查看如偏
    * */
    @RequestMapping("/detialImg/{id}")
    public String detailImg(@PathVariable("id") int id,Model model){
        Pic pic = picService.selectById(id);
        model.addAttribute("item",pic);
        return PREFIX + "picdetail.html";
    }

}
